;
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/07/16
 * Time: 10:12
 * @link http://www.lmterp.cn
 */
layui.define(['jquery', 'layer'], function(exports)
{
    "use strict";
    var $ = layui.jquery
        , layer = layui.layer
        // 外部接口
        , printPDF = {
            config: {
                ip: "127.0.0.1" // HttpPrinter IP 地址
                , port: 12345 // HttpPrinter 端口
                , token: "aa" //  HttpPrinter 通信 token
                , preview: 0 //  是否打印预览
                , paperSize: "custom" // 指定打印纸张，打印纸张类型(A3 A4 A5 B4 B5 custom)
                , orientation: "1" // 方向：1=纵向，2=横向
                , printList: [] // 打印机列表
            }
            //设置全局项
            , set: function(options)
            {
                var that = this;
                that.config = $.extend({}, that.config, options);
                return that;
            }
            // 初始化
            , init: function(options)
            {
                this.config.printList = this.getPrintList();
            }
        };

    /**
     * 获取 HttpPrinter 地址
     * @returns {string}
     */
    printPDF.getUrl = function()
    {
        return "http://" + this.config.ip + ":" + this.config.port + "/";
    };

    /**
     * 打印机是否存在
     * @param {string} name 打印机名称
     * @returns {boolean}
     */
    printPDF.hasPrint = function(name)
    {
        return $.inArray(name, this.config.printList) >= 0;
    };

    /**
     * 打印 pdf
     * @param {string} pdfUrl 文件地址绝对地址，需以 http 或者 https 开头
     * @param {string} name 打印机名称, 默认使用系统默认打印机
     * @param {number} qty 打印数量，默认1份
     * @returns {boolean}
     */
    printPDF.doPrint = function(pdfUrl, name, qty)
    {
        if(!pdfUrl)
        {
            layer.msg("未指定打印 pdf 地址", {icon: 5, time: 2000});
            return false;
        }
        var json = {
            "method": "pdfprint", // 打印事件
            "Preview": this.config.preview,
            "token": this.config.token,
            "PdfUrl": pdfUrl, // pdf 路径
            "PrinterName": this.hasPrint(name) ? name : "", // 指定打印机名称，未指定则使用默认打印机
            "PaperSize": this.config.paperSize || "custom", // 指定打印纸张
            "PaperLength": "0", // 自定义高
            "PaperWidth": "0", // 自定义宽
            "Copies": qty || "1", // 打印份数，默认1份
            "Orientation": this.config.orientation
        };
        $.ajax({
            async: false,
            url: this.getUrl(),
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            data: JSON.stringify(json),
            success: function (data) {
                if (data.status != "ok") {
                    layer.msg("打印失败:" + data.data, {icon: 5, time: 2000});
                    return false;
                }
            },
            error: function (data) {
                // console.log(JSON.stringify(data));
                layer.msg("连接HttpPrinter失败:" + JSON.stringify(data), {icon: 5, time: 2000});
            }
        });
    };

    /**
     * 获取打印机列表
     * @returns {[]}
     */
    printPDF.getPrintList = function()
    {
        var json = {
            "method": "getprinterlist",
            "token": this.config.token
        };
        var printList = [];
        $.ajax({
            async: false, // 改为同步执行
            url: this.getUrl(),
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            data: JSON.stringify(json),
            success: function(response)
            {
                // console.log(JSON.stringify(response));
                if(response.status == "ok")
                {
                    for(var i in response.data)
                    {
                        printList.push(response.data[i].name.replace(/\s/g, ''));
                    }
                }
            },
            error: function(data)
            {
                layer.msg("连接失败请检查【HttpPrinter】是否启动，错误信息【" + JSON.stringify(data) + "】", {icon: 5, time: 2000});
            }
        });
        return printList;
    };

    printPDF.init();
    exports('printPDF', printPDF);
});